//
//  WeeklyLetterItemView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/2/22.
//

import SwiftUI
import Kingfisher

//每周来信
struct WeeklyLetterItemView: View {
    @EnvironmentObject var themeManager: ThemeManager
    @State var item: WeeklyLetterBean
    @StateObject var viewModel = WeeklyLetterViewModel()
    @State private var leftViewHeight : CGFloat = 130
    @State private var showContent = false //显示内容
    @State var read : Bool = false //是否已读
    
    var body: some View {
        HStack {
            VStack(alignment:.leading,spacing: 0){
                if (item.title ?? "").isEmpty == true {
                    HStack(alignment: .firstTextBaseline, spacing: 0){
                        Text(TimeUtils.getDate(time: item.time, format: "M"))
                            .font(.custom(IFontType.number.rawValue, size: 28))
                            .foregroundStyle(Color.hex("000000"))
                            .fontWeight(.medium)
                        Text("月")
                            .font(.system(size: 10))
                            .foregroundStyle(Color.hex("000000"))
                        Text("第\(TimeUtils.getMonthWeek(time: TimeInterval(item.time)))周")
                            .font(.system(size: 12))
                            .foregroundStyle(Color.hex("a5a5a5"))
                            .padding(.leading, 10)
                    }
                } else {
                    Text(item.title ?? "")
                        .font(.system(size: 18))
                        .foregroundStyle(Color.hex("000000"))
                        .fontWeight(.medium)
                        .padding(.top, 10)
                }
                Text(TimeUtils.getDate(time: item.time, format: "M月d号 H:mm"))
                    .font(.system(size: 12))
                    .foregroundStyle(Color.hex("a5a5a5"))
                    .padding(.top,5)
                HStack(spacing:5){
                    KFImage(URL(string: item.avatar.formatCdn()))
                        .resizable()
                        .placeholder {
                            Image(.appDefaultLogo)
                                .resizable()
                                .aspectRatio(contentMode: .fill)
                                .padding(4)
                                .background(.bgItemGray)
                        }
                        .aspectRatio(contentMode: .fill)
                        .frame(width: 20,height: 20)
                        .clipShape(Circle())
                    Text(item.name ?? "")
                        .font(.system(size: 12))
                        .foregroundStyle(.gray)
                }.padding(.top, 30)
            }
            .padding(.top,10)
            .padding(.bottom,15)
            .padding(.horizontal,20)
            Spacer()
            if read == false {
                Image(.homeIconLetter)
                    .resizable()
                    .aspectRatio(contentMode: .fit) // 保持原始宽高比，完全显示图片（不裁剪）
                    .frame(width: 70)
                    .padding(.trailing, 20)
            } else {
                Image(.homeIconLetter2)
                    .resizable()
                    .aspectRatio(contentMode: .fit) // 保持原始宽高比，完全显示图片（不裁剪）
                    .frame(width: 70)
                    .padding(.trailing, 20)
            }
            
            //下面的旧的已读未读，暂时隐藏
            //            ZStack {
            //                HStack(spacing: 0) {
            //                    Text(item.content ?? "")
            //                        .font(.system(size: 12))
            //                        .foregroundStyle(themeManager.themeColor)
            //                        .padding().blur(radius: read ? 0 : 3)
            //                }
            //                .frame(maxWidth: .infinity,maxHeight: .infinity, alignment: .topLeading)
            //                ZStack {
            //                    if read == false {
            //                        Image(.homeIconMail)
            //                            .resizable()
            //                            .frame(width: 40, height: 40)
            //                        Color.red.frame(width: 5,height: 5)
            //                            .clipShape(Circle())
            //                            .padding(.leading, 30)
            //                            .padding(.bottom,28)
            //                    }
            //                }
            //            }.frame(width: leftViewHeight * 1.2, height: leftViewHeight)
            //                .background(themeManager.themeColor.opacity(0.3))
            //                .cornerRadius(20)
        }/*.frame(maxWidth: .infinity,maxHeight: leftViewHeight)*/
        .frame(maxWidth: .infinity)
        .background(Color.hex("f6f6f6"))
        .cornerRadius(20)
        .contentShape(Rectangle())
        .onTapGesture {
            //                showContent = true //弹窗类型，暂时关闭
            if item.read {
                pathManager.jump(type: JumpType.WEEKLY_LETTER_DETAIL, data: item)
            } else {
                commonManager.showWeeklyLetterOpen(true, item)
                viewModel.doRead(read: $read, bean: $item)
            }

        }
        .sheet(isPresented: $showContent) {
            //弹窗类型的详情，暂时关闭
            WeeklyLetterContentView(bean: item).presentationDetents([.fraction(0.9), .large])
        }.onChange(of: showContent) { newValue in
            if !newValue {
                viewModel.doRead(read: $read, bean: $item)
            }
        }.onAppear{
            read = item.read
        }
    }
}


//#Preview {
//    WeeklyLetterItemView(item: WeeklyLetterBean())
//}


//定义预览提供者
struct WeeklyLetterItemView_Previews: PreviewProvider {
    static var previews: some View {
        //使用预览共享配置
        configurePreviewEnvironment(for: WeeklyLetterItemView(item: WeeklyLetterBean()))
    }
}
